import os

from elasticsearch import Elasticsearch, helpers
from langchain_core.tracers import langchain
from langchain_openai import OpenAIEmbeddings

from backend.app.script.db_data_source import DataSourceByExcel
from backend.app.services.goods.goods import mapping

os.environ["OPENAI_API_KEY"] = "sb-e7c958d39c75ff3da4eff9cd47c14e95c9cf1793722bad97"
os.environ["OPENAI_API_BASE"] = "https://api.openai-sb.com/v1"
langchain.debug = True
embedding = OpenAIEmbeddings()

client = Elasticsearch(
    "https://8.141.5.252:9200",
    basic_auth=("elastic", "gVq8w5xmEY36Z484mHPe"),
    ssl_assert_fingerprint="D4:95:40:EA:09:28:CB:D2:A8:69:C1:82:C1:B9:B1:A5:60:8B:6D:63:A6:01:BF:75:E0:73:48:73:4C:08:9B:55",
    max_retries=10)


def check_exist(index_name: str):
    if not client.indices.exists(index=index_name):
        client.indices.create(index=index_name, body=mapping)


async def insert_by_file(path: str, index_name: str, start_page: int = 1, page_size=100):
    """
    根据文件路径插入excel数据
    :param path:
    :param index_name: 索引名称,platform_xxx
    :param start_page: 开始页码
    :param page_size: 页码大小
    :return:
    """
    # index_name = "platform_hushan"
    check_exist(index_name)
    #  读取数据
    td = DataSourceByExcel()
    df = await td.read_excel_es(path)

    #  获取要插入的数据
    pages = td.get_pages(df.shape[0], page_size=page_size)
    #  分页插入数据
    for page in range(start_page, pages + 1):
        print(f"[insert_by_file] 总共{pages}页， 第{page} 开始获取当页数据.....")
        device_list = await td.get_device_schema(df, page=page, page_size=page_size)
        print(f"[insert_by_file] 总共{pages}页， 第{page} 完成数据获取.....")

        goods_list = []
        for good in device_list:
            #  向量化
            text = good.to_vector_desc()
            vector = embedding.embed_query(text)
            metadata = good.model_dump(by_alias=True)
            body = dict(text=text, embedding=vector, metadata=metadata)
            item = {
                "_op_type": "update",
                "_index": index_name,
                "_id": good.gid,
                "doc": body,
                "doc_as_upsert": True  # 如果文档不存在，插入它
            }
            goods_list.append(item)

        # 将数据转换为结构
        try:
            res = helpers.bulk(client, goods_list)
            print(f"[insert_by_file] 总共{pages}页， 第{page} 页数据插入完成....", res)
        except Exception as e:
            print(f"[insert_by_file] 总共{pages}页， 第{page} 页数据插入失败....", str(e))
            continue
    print("页面大小：", page_size)
    return
